package com.example.expandabledemo.expand;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.expandabledemo.MainAdapter;
import com.example.expandabledemo.R;
import com.example.expandabledemo.view.ExpandableLayout;

import java.util.ArrayList;
import java.util.List;


public class HorizontalFragment extends Fragment implements View.OnClickListener, ExpandableLayout.OnExpansionUpdateListener {
    private ExpandableLayout expandableLayout;
    private ImageView expandButton;
    private RecyclerView recyclerView;
    private MainAdapter mAdapter;
    private List<String> contacts = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.horizontal_fragment, container, false);
        initData();
        expandableLayout = rootView.findViewById(R.id.expandable_layout);
        expandButton = rootView.findViewById(R.id.expand_button);
        recyclerView = rootView.findViewById(R.id.rv_h);
        /***横向***/
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.HORIZONTAL));
        mAdapter = new MainAdapter(getContext(), contacts);
        recyclerView.setAdapter(mAdapter);

        expandableLayout.setOnExpansionUpdateListener(this);
        expandButton.setOnClickListener(this);

        return rootView;
    }

    @Override
    public void onExpansionUpdate(float expansionFraction, int state) {
        Log.d("ExpandableLayout", "State: " + state);
        expandButton.setRotation(expansionFraction * 180);
    }

    @Override
    public void onClick(View view) {
        expandableLayout.toggle();
    }
    private void initData() {
        contacts.add("Amanda M1");
        contacts.add("Anquises2");
        contacts.add("Bellisima3");
        contacts.add("Bodo4");
        contacts.add("Brogan5");
        contacts.add("Brogan6");
        contacts.add("Brogan7");
        contacts.add("Brogan8");
        contacts.add("Brogan9");
    }
}
